package com.hm.ibsm.service;

import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.TypeReference;
import com.hm.common.network.httpclient.HttpClientFactory;
import com.hm.common.network.httpclient.HttpClientResponseParse;
import com.hm.common.su.ServerResponseParse;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.ServerResponse;
import com.hm.ibsm.manage.vo.role.RoleVo;
import com.hm.ibsm.util.LocationConfig;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年3月9日 上午11:56:45
 * @version 1.0
 * @describe
 */
@Slf4j
@Component
public class RoleService {

	@Autowired
	private LocationConfig locationConfig;

	private static String MODEL_NAME = RoleService.class.getName();

	private static String QUERY_ROLE_LIST = "/role/{0}/{1}?roleName={2}";

	private static String ADD_ROLE = "/role/";

	private static String UPDATE_ROLE = "/role/{0}";

	public PageInfo<RoleVo> query(Integer pageNumber, Integer pageSize, RoleVo role) {
		try {
			HttpResponse response = locationConfig
					.accredit(HttpClientFactory.GET.build(locationConfig.mgUri(QUERY_ROLE_LIST), pageNumber, pageSize, role.getRoleName())).execute();
			return ServerResponseParse.parse(HttpClientResponseParse.parseGeneric(response, new TypeReference<ServerResponse<PageInfo<RoleVo>>>() {
			}));
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
		}
		return PageInfo.empty();
	}

	public RoleVo findRole(String roleId) throws Exception {
		HttpResponse response = locationConfig.accredit(HttpClientFactory.GET.build(locationConfig.mgUri(UPDATE_ROLE), roleId)).execute();
		return ServerResponseParse.parse(HttpClientResponseParse.parseGeneric(response, new TypeReference<ServerResponse<RoleVo>>() {
		}));
	}

	public boolean addRole(RoleVo role) {
		try {
			HttpResponse response = locationConfig.accredit(HttpClientFactory.POST.build(locationConfig.mgUri(ADD_ROLE))).parameters(role).execute();
			return ServerResponseParse.parse(HttpClientResponseParse.parseGeneric(response, new TypeReference<ServerResponse<Boolean>>() {
			}));
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
		}
		return false;
	}

	public boolean updateRole(String roleId, RoleVo role) {
		try {
			HttpResponse response = locationConfig.accredit(HttpClientFactory.PUT.build(locationConfig.mgUri(UPDATE_ROLE), roleId)).parameters(role)
					.execute();
			return ServerResponseParse.parse(HttpClientResponseParse.parseGeneric(response, new TypeReference<ServerResponse<Boolean>>() {
			}));
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
		}
		return false;
	}

	public boolean deleteRole(String roleId) {
		try {
			HttpResponse response = locationConfig.accredit(HttpClientFactory.DELETE.build(locationConfig.mgUri(UPDATE_ROLE), roleId)).execute();
			return ServerResponseParse.parse(HttpClientResponseParse.parseGeneric(response, new TypeReference<ServerResponse<Boolean>>() {
			}));
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
		}
		return false;
	}
}
